﻿/*******************************************************************************
 * Copyright © 2016 LMG 版权所有
 * Website：http://www.lmg.com
 * Author: Luomg
 * CreateDate: 2023年10月22日 
 * Description: 角色和菜单关联表 

*********************************************************************************/
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Text;
using System.Collections.Generic;
using System.Data;
using Lmg.Common;
using Lmg.Model;

namespace Lmg.WebApi.Controllers
{
    /// <summary>
    /// 角色和菜单关联表 Controller
    /// </summary>
    [Verify]
	[Route("api/[controller]/[action]")]
	[Tags("角色和菜单关联表 ")]
    [ApiExplorerSettings(GroupName = "sys")]
    public class sys_role_deptController : BaseController
    {
        /// <summary>
        /// 角色和菜单关联表接口
        /// </summary>
        private readonly Isys_role_deptService _sys_role_deptService;

        public sys_role_deptController(Isys_role_deptService sys_role_deptService)
        {
            _sys_role_deptService = sys_role_deptService;
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ActionPermissionFilter(Permission = "system:sys_role_dept:list")]
        public IActionResult list([FromQuery] sys_role_dept parm, PagerInfo pager)
        {
            var predicate = Expressionable.Create<sys_role_dept>();

            //predicate = predicate.AndIF(!parm.ConfigType.IsEmpty(), m => m.ConfigType == parm.ConfigType);

            var response = _sys_role_deptService.GetPages(predicate.ToExpression(), pager);

            return Content(response.ToJSON(), "application/json");
        }

        /// <summary>
        /// 查询单个
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet]
        [ActionPermissionFilter(Permission = "system:sys_role_dept:query")]
        public IActionResult get(int Id)
        {
            var response = _sys_role_deptService.GetId(Id);

            return SUCCESS(response);
        }
		
        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionPermissionFilter(Permission = "system:sys_role_dept:add")]
        [Log(Title = "角色和菜单关联表", BusinessType = BusinessType.INSERT)]
        public IActionResult add([FromForm] sys_role_dept parm)
        {
            if (parm == null)
            {
                throw new CustomException("请求参数错误");
            }
            //var model = parm.Adapt<SysConfig>().ToCreate(HttpContext);

            return SUCCESS(_sys_role_deptService.Insertable(parm));
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionPermissionFilter(Permission = "system:sys_role_dept:update")]
        [Log(Title = "角色和菜单关联表", BusinessType = BusinessType.UPDATE)]
        public IActionResult Update([FromForm] sys_role_dept parm)
        {
            if (parm == null)
            {
                throw new CustomException("请求实体不能为空");
            }
			
            var response = _sys_role_deptService.Update(parm);

            return SUCCESS(response);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionPermissionFilter(Permission = "system:sys_role_dept:remove")]
        [Log(Title = "角色和菜单关联表", BusinessType = BusinessType.DELETE)]
        public IActionResult remove(string ids)
        {
            int[] idsArr = Tools.SpitIntArrary(ids);
            if (idsArr.Length <= 0) { return ToResponse(ApiResult.Error($"删除失败Id 不能为空")); }

            var response = _sys_role_deptService.Delete(idsArr);

            return SUCCESS(response);
        }
		/// <summary>
        ///  导入模板
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpGet]
        [Log(Title = "角色和菜单关联表导入模板", BusinessType = BusinessType.IMPORT, IsSaveRequestData = true, IsSaveResponseData = false)]
        [AllowAnonymous]
        public IActionResult importTemplate()
        {
            (string, string) result = DownloadImportTemplate("sys_role_dept");
             return ExportExcel(result.Item2, result.Item1);
        }
		/// <summary>
        ///  角色和菜单关联表导入
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        [Log(Title = "角色和菜单关联表导入", BusinessType = BusinessType.IMPORT, IsSaveRequestData = false, IsSaveResponseData = true)]
        [ActionPermissionFilter(Permission = "system:sys_role_dept:import")]
        public IActionResult importData([FromForm(Name = "file")] IFormFile formFile)
        {
		    List<sys_role_dept> list = new();
			using (var stream = formFile.OpenReadStream())
			{
				list = stream.Query<sys_role_dept>(startCell: "A2").ToList();
			}

			return SUCCESS(_sys_role_deptService.Insert(list));
        }
		/// <summary>
        /// 导出
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ActionPermissionFilter(Permission = "system:sys_role_dept:export")]
        [Log(Title = "角色和菜单关联表", BusinessType = BusinessType.EXPORT)]
        public IActionResult export([FromForm] sys_role_dept parm)
        {
            var list = _sys_role_deptService.GetList();
			var result = ExportExcelMini(list, "sys_role_dept", "角色和菜单关联表列表");
			return ExportExcel(result.Item2, result.Item1);
        }
    }
}
